<?php 

class AdminArticlePresenter extends AdminPresenter{
	
	private $model;
	/**
	 * Start lifecycle presenter
	 *
	 * @return void
	 */
	protected function startup(){
		parent::startup();
		$this->template->jsFile[] = "inc/news.js";
		$this->model = ArticleModel::getInstance();
	}

	/**
	 * Settings for all render
	 *
	 * @return void
	 */
	protected function beforeRender(){

	}

	/**
	 * Render default action
	 *
	 * @return void
	 */
	public function renderDefault($id){
		
		$this->template->title = "Přehled článků";
		$this->template->id = $id;
	}
	
	protected function createComponentGrid($name){
		
	    $grid = new DataGrid;
	    $grid->bindDataTable($this->model->getDataSource());
	    
	    $grid->addColumn("title", "Nadpis")->addFilter();
	    $grid->addDateColumn('date', 'Datum', "%d.%m.%Y");
	    //$grid->addColumn("content", "Obsah", 50)->addFilter();
	    $grid->addColumn("perex", "Perex")->addFilter();
	    
	    // nastavíme klíč pro akce (a také i pro operace, o těch později)
		$grid->keyName = 'id_article';
		
		// přidáme sloupec pro akce (sloupců může být i více)
		$grid->addActionColumn('Actions');
		
		// a naplníme datagrid akcemi pomocí továrničky
		$grid->addAction('Nový', 'addNews', Html::el('span')->class('icon icon-edit'), $useAjax = FALSE);
		$grid->addAction('Smaž', 'deleteNews', Html::el('span')->class('icon icon-del'), $useAjax = TRUE);
			    
	    $this->addComponent($grid, "grid");
	}
	
	public function actionAddNews($id){
		
		$menuItem = $this->system->getMenuItemByName($id);
		
		$this->template->title = "Přidat článek";
		$this->template->edit = $this->model->getNewById($_GET["id_article"]);
		$this->template->picture = $this->model->getPicture($this->template->edit["id_file"]);
		$this->template->id = $id;
		$this->template->id_menu = $menuItem[0]["id_menu"];
	}
	
	
	public function actionSaveNew(){
		
		$values = $_POST;
		$values['date'] = new DateTime();
		
		$values['seo_title'] = $this->model->createSeo($values["title"]);
		
		$this->model->saveNews($values);
		$this->flashMessage("Článek byl uložen.", "ok");
		$this->redirect("AdminArticle:default", $this->getParam("id"));
	}
	
	public function actionDeleteNews($id_news){
		
		$this->model->deleteNews($_GET["id_article"]);
		$this->flashMessage("Článek byl smazán.", 'ok');
		$this->redirect("AdminArticle:default", $this->getParam("id"));
	}
}

?>
